package org.tis.tools.abf.module.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.abf.module.sys.controller.request.SysDictDefaultValueRequest;
import org.tis.tools.abf.module.sys.controller.request.SysDictDelRequest;
import org.tis.tools.abf.module.sys.controller.request.SysDictFilterQueryRequest;
import org.tis.tools.abf.module.sys.dao.SysDictMapper;
import org.tis.tools.abf.module.sys.entity.SysDict;
import org.tis.tools.abf.module.sys.entity.SysDictItem;
import org.tis.tools.abf.module.sys.entity.enums.DictFromType;
import org.tis.tools.abf.module.sys.entity.enums.DictType;
import org.tis.tools.abf.module.sys.entity.vo.SysDictApiDetail;
import org.tis.tools.abf.module.sys.entity.vo.SysDictDetail;
import org.tis.tools.abf.module.sys.entity.vo.SysDictHandelDetail;
import org.tis.tools.abf.module.sys.entity.vo.SysDictItemDetail;
import org.tis.tools.abf.module.sys.exception.SYSExceptionCodes;
import org.tis.tools.abf.module.sys.exception.SysManagementException;
import org.tis.tools.abf.module.sys.service.ISysDictItemService;
import org.tis.tools.abf.module.sys.service.ISysDictService;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.model.exception.i18.ExceptionCodes;

import java.math.BigDecimal;
import java.util.*;

import static org.tis.tools.core.utils.BasicUtil.wrap;
/**
 * sysDict的Service接口实现类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDictServiceImpl  extends ServiceImpl<SysDictMapper,SysDict> implements ISysDictService {

    @Autowired
    private ISysDictItemService iSysDictItemService;


    @Override
    public SysDict addDict(SysDict sysDict) throws SysManagementException {

        if(StringUtils.isNotBlank(sysDict.getGuidParents())){
            SysDict sysDictNew = selectById(sysDict.getGuidParents());
            if (sysDictNew == null){
                throw new SysManagementException(ExceptionCodes.NOT_FOUND_WHEN_QUERY,
                        wrap(SYSExceptionCodes.NOT_FOUND_SYS_DICT_WITH_GUID,sysDict.getGuidParents())
                );
            }
        }

        if (null == sysDict.getSeqno()){
            sysDict.setSeqno(new BigDecimal(this.baseMapper.maxSeqNo()+1));
        }else {
            List<SysDict> sysDictList = this.baseMapper.queryBySeqNo(sysDict.getSeqno());
            if (0 != sysDictList.size()){
                sysDict.setSeqno(new BigDecimal(this.baseMapper.maxSeqNo()+1));
            }
        }

        insert(sysDict);
        return sysDict;
    }

    @Override
    public SysDict queryDictDetail(String id) throws SysManagementException {
        return null;
    }

    @Override
    public List<SysDict> queryDict(String id) throws SysManagementException {

        EntityWrapper<SysDict> wrapper = new EntityWrapper<>();
        wrapper.eq(SysDict.COLUMN_DICT_KEY,id);
        return selectList(wrapper);
    }

    @Override
    public SysDict editSysDict(SysDict sysDict) throws SysManagementException {

        EntityWrapper<SysDict> wrapper = new EntityWrapper<>();
        wrapper.eq(SysDict.COLUMN_GUID,sysDict.getGuid());
        if(selectOne(wrapper) == null){
            throw new SysManagementException(
                    ExceptionCodes.NOT_FOUND_WHEN_QUERY,
                    wrap(SysDictItem.COLUMN_GUID,sysDict.getGuid()),sysDict.getGuid());
        }

        if (null == sysDict.getSeqno()){
            sysDict.setSeqno(new BigDecimal(this.baseMapper.maxSeqNo()+1));
        }else {
            List<SysDict> sysDictList = this.baseMapper.queryBySeqNoFilter(sysDict.getSeqno(),sysDict.getGuid());
            if (0 != sysDictList.size()) {
                sysDict.setSeqno(new BigDecimal(this.baseMapper.maxSeqNo() + 1));
            }
        }

        update(sysDict,wrapper);
        return sysDict;
    }

    @Override
    public void deleteDict(String id) throws SysManagementException {

        //删除该节点对应的子节点数据
         deleteAllChild(id);
    }

    /**
     * 根据业务字典guid批量删除业务字典
     */
    @Override
    public List<SysDict> deleteDictList(SysDictDelRequest sysDictDelRequest) throws SysManagementException {

        List<SysDict> delList = new ArrayList<SysDict>();

        List<String> dictGuidList = sysDictDelRequest.getDeleteList();
        //判断列表中的guid所对应的业务字典都存在,并且没有系统级的业务字典
        if (null != dictGuidList || 0 != dictGuidList.size()){
            for (String dictGuid : dictGuidList){
                if (!StringUtil.isEmpty(dictGuid)){
                    SysDict sysDict =selectById(dictGuid);
                    if (null == sysDict){
                        throw new SysManagementException(SYSExceptionCodes.NOT_FOUND_SYS_DICT_WITH_GUID,wrap
                                ("找不到对应记录或已经被删除"));
                    }
                    if (DictType.SYS.equals(sysDict.getDictType())){
                        throw new SysManagementException(SYSExceptionCodes.FAILURE_DELETE_SYS_DICT_LIST_WHEN_DICTTYPE_IS_SYS,wrap
                                ("系统级字典不允许删除"));
                    }
                    delList.add(sysDict);
                }
            }
        }else {
            throw new SysManagementException(SYSExceptionCodes.LAKE_PARMS_FOR_DELETE_SYS_DICT_LIST,wrap("批量删除时参数为空!"));
        }

        //批量删除业务字典
        for(String dictGuid : dictGuidList) {
            if (!StringUtil.isEmpty(dictGuid)) {
                deleteAllChild(dictGuid);
            }
        }

        return delList;
    }

    private void deleteAllChild(String id) throws SysManagementException{

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.eq(SysDict.COLUMN_GUID_PARENTS,id);
        //获取子列表
        List<SysDict> list = selectList(wrapper);

        if (0 == list.size() || null == list){
            deleteById(id);
            deleteItem(id);
        }else {
            for (SysDict sysDict : list){
                if (sysDict != null){
                    deleteAllChild(sysDict.getGuid());
                    deleteItem(id);
                }
            }
            deleteById(id);
            deleteItem(id);
        }
    }

    public void deleteItem(String id) throws SysManagementException{

        Wrapper<SysDictItem> wrapper = new EntityWrapper<SysDictItem>();
        wrapper.eq(SysDictItem.COLUMN_GUID_DICT,id);
        List<SysDictItem> list = iSysDictItemService.selectList(wrapper);
        if (0 != list.size()){
           iSysDictItemService.delete(wrapper);
        }
    }


    @Override
    public Page<SysDict> querySysDicts(Page<SysDict> page, Wrapper<SysDict> wrapper){
        return selectPage(page,wrapper);
    }

    /**
     * 查询所有的父业务字典,并不分页
     */
    @Override
    public List<SysDict> queryParentList() throws SysManagementException {

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.ne(SysDict.COLUMN_GUID_PARENTS,"");
        List<SysDict> listSon = selectList(wrapper);


        Wrapper<SysDict> wrapper1 = new EntityWrapper<SysDict>();
        List<SysDict> listAll = selectList(wrapper1);

        List<SysDict> listParent = new ArrayList<SysDict>();

        for (SysDict sysDictAll :listAll){
            String GuidAll = sysDictAll.getGuid();
            Boolean  isexist = false;
            for (SysDict sysDictSon : listSon){
                if (GuidAll.equals(sysDictSon.getGuid())){
                    isexist = true;
                    break;
                }
            }
            if (!isexist){
                listParent.add(sysDictAll);
            }
        }
        return listParent;
    }

    @Override
    public SysDict queryOneSysDictByGuid(String id) throws SysManagementException {

        EntityWrapper<SysDict> wrapper = new EntityWrapper<>();
        wrapper.eq(SysDict.COLUMN_GUID,id);
        return selectOne(wrapper);
    }

    @Override
    public List<SysDict> dictKeyQuery(SysDict sysDict) throws SysManagementException {

        EntityWrapper<SysDict> wrapper = new EntityWrapper<>();
        if(sysDict.getDictKey()!= null && sysDict.getDictKey()!= "" ){
            wrapper.eq(SysDict.COLUMN_DICT_KEY,sysDict.getDictKey());
        }
        if(sysDict.getDictName()!=null && sysDict.getDictName()!= ""){
            wrapper.eq(SysDict.COLUMN_DICT_NAME,sysDict.getDictName());
        }

        List<SysDict> list = selectList(wrapper);
        return list;
    }

    @Override
    public SysDict querySysDictByGuid(String id) throws SysManagementException {

        EntityWrapper<SysDict> wrapper = new EntityWrapper<>();
        wrapper.eq(SysDict.COLUMN_GUID,id);

        SysDict sysDict = selectOne(wrapper);
        return sysDict;
    }

    /**
     * 查询业务字典的树结构
     */
    @Override
    public SysDictHandelDetail queryDictTree(String id) throws SysManagementException {

        SysDictHandelDetail sysDictDetail = new SysDictHandelDetail();

        //查询该业务字典
        SysDict sysDictOne = selectById(id);

        List<Object> list = new ArrayList<Object>();
        //字典项情况下
        if (DictFromType.DICT_ITEM.toString().equals(sysDictOne.getFromType().toString())){

            List<SysDictHandelDetail> allDictList = this.baseMapper.queryAllDict();
            List<SysDictHandelDetail> allItemList = this.baseMapper.queryAllDictItem();
            //将两个集合中的数据放在一个集合内
            allDictList.addAll(allItemList);

            if (0 != allDictList.size()){
                Map<String,SysDictHandelDetail> map = setInSysDictHandelDetail(allDictList,sysDictOne);
                sysDictDetail = map.get(id);
            }else {
                sysDictDetail = setValueToDetail(sysDictOne,list);
            }
        }
        //单表情况下
        if (DictFromType.SINGLE_TABLE.toString().equals(sysDictOne.getFromType().toString())){

            String sql = "select "+sysDictOne.getUseForKey() + "," + sysDictOne.getUseForName() +" from " +
                    ""+sysDictOne.getFromTable()+" where data_status = '0'";

            List<LinkedHashMap<String,Object>> listQue = this.baseMapper.queryForSigleTable(sql) ;

            setObjectToItem(list,listQue,sysDictOne);

            sysDictDetail = setValueToDetail(sysDictOne,list);
        }
        //多表情况下
        if (DictFromType.TABLES_OR_VIEW.toString().equals(sysDictOne.getFromType().toString())){

            List<LinkedHashMap<String,Object>> listQue = this.baseMapper.queryForSigleTable(sysDictOne.getSqlFilter()) ;

            setObjectToItem(list,listQue,sysDictOne);

            sysDictDetail = setValueToDetail(sysDictOne,list);
        }

        return sysDictDetail;
    }

 private Map<String,SysDictHandelDetail> setInSysDictHandelDetail(List<SysDictHandelDetail> handelDetailList,SysDict sysDictOne){

     Map<String,SysDictHandelDetail> map = new HashMap<String,SysDictHandelDetail>();
     for (SysDictHandelDetail handelDetail : handelDetailList){
         if (null != handelDetail){
             map.put(handelDetail.getGuid(),new SysDictHandelDetail(handelDetail,new ArrayList<Object>()));
         }
     }

     Map<String,SysDictHandelDetail> exitMap = new HashMap<String, SysDictHandelDetail>();
     for(String key:map.keySet())
     {
         if (null != map.get(map.get(key).getGuidParent())){
             map.get(map.get(key).getGuidParent()).getChildren().add(map.get(map.get(key).getGuid()));
         } else {
             SysDictHandelDetail handelDetailExit = map.get(map.get(key).getGuid());
             exitMap.put(handelDetailExit.getGuid(),handelDetailExit);
         }
         //将本次查询需要的对象放进map中
         if (sysDictOne.getGuid().equals(key)) {
             SysDictHandelDetail handelDetailExit = map.get(map.get(key).getGuid());
             exitMap.put(handelDetailExit.getGuid(), handelDetailExit);
         }

     }

     return exitMap;
 }

    //将查询回来的map遍历,并生成sysDictItemDetail对象
    private void setObjectToItem(List<Object> listObject,List<LinkedHashMap<String,Object>> listQue,SysDict sysDictOne){
        for (LinkedHashMap<String,Object> map :listQue){
            if (null != map){
                SysDictItem sysDictItem = new SysDictItem();
                Iterator iter = map.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    Object key = entry.getKey();
                    Object val = entry.getValue();

                    if (sysDictOne.getUseForKey().equals(key.toString())){
                        sysDictItem.setItemValue(val.toString());
                    }
                    if (sysDictOne.getUseForName().equals(key.toString())){
                        sysDictItem.setItemName(val.toString());
                    }
                }
                SysDictItemDetail sysDictItemDetail = new SysDictItemDetail(sysDictItem,sysDictOne
                        .getDictType(),sysDictOne.getFromType());

                listObject.add(sysDictItemDetail);
            }
        }
    }

    /**
     * 过滤查询业务字典的树结构
     */
    @Override
    public SysDictHandelDetail filterQueryDictTree(SysDictFilterQueryRequest filterQueryRequest) throws SysManagementException {

        SysDictHandelDetail sysDictDetail = new SysDictHandelDetail();

        //查询该业务字典
        SysDict sysDictOne = selectById(filterQueryRequest.getGuid());

        List<Object> list = new ArrayList<Object>();

        //字典项情况下
        if (DictFromType.DICT_ITEM.toString().equals(sysDictOne.getFromType().toString())) {

            List<SysDictHandelDetail> allDictList = this.baseMapper.queryAllDict();
            List<SysDictHandelDetail> allItemList = this.baseMapper.queryAllDictItem();
            //将两个集合中的数据放在一个集合内
            allDictList.addAll(allItemList);

            if (StringUtil.isEmpty(filterQueryRequest.getDictName())) {
                if (0 != allDictList.size()) {
                    Map<String, SysDictHandelDetail> map = setInSysDictHandelDetail(allDictList,sysDictOne);
                    sysDictDetail = map.get(filterQueryRequest.getGuid());
                } else {
                    sysDictDetail = setValueToDetail(sysDictOne,list);
                }
            } else {
                //所有根据名称匹配到的数据
                List<SysDictHandelDetail> dictList = this.baseMapper.queryAllDictByName("%"+filterQueryRequest
                        .getDictName()+"%");
                List<SysDictHandelDetail> itemList = this.baseMapper.queryAllDictItemByName("%"+filterQueryRequest
                        .getDictName()+"%");
                //将两个集合中的数据放在一个集合内
                dictList.addAll(itemList);

                Map<String, SysDictHandelDetail> allMap = new HashMap<String, SysDictHandelDetail>(allDictList.size());
                if (0 != allDictList.size()) {
                    //查询结果非空,则循环拼接
                    for (SysDictHandelDetail sysDictHandelDetail : allDictList) {
                        if (null != sysDictHandelDetail) {
                            allMap.put(sysDictHandelDetail.getGuid(), sysDictHandelDetail);
                        }
                    }
                }

                //找出所有需要排序的节点信息
                List<SysDictHandelDetail> resultList = new ArrayList<SysDictHandelDetail>();
                if (0 != dictList.size()) {
                    for (int i = 0; i < dictList.size(); i++) {
                        if (StringUtil.isNotEmpty(dictList.get(i).getGuidParent())) {
                            SysDictHandelDetail dictHandelDetailSon = dictList.get(i);
                            resultList.add(dictHandelDetailSon);
                            SysDictHandelDetail dictHandelDetailFather = dictHandelDetailSon;
                            while (StringUtil.isNotEmpty(allMap.get(dictHandelDetailSon.getGuidParent()).getGuidParent())) {
                                dictHandelDetailFather = allMap.get(dictHandelDetailSon.getGuidParent());
                                resultList.add(dictHandelDetailFather);
                                dictHandelDetailSon = dictHandelDetailFather;
                            }
                            resultList.add(allMap.get(dictHandelDetailFather.getGuidParent()));
                        }
                    }

                    if (0 != resultList.size()) {
                        Map<String, SysDictHandelDetail> map = setInSysDictHandelDetail(resultList,sysDictOne);
                        sysDictDetail = map.get(filterQueryRequest.getGuid());
                        if (null == sysDictDetail){
                            sysDictDetail = setValueToDetail(sysDictOne,list);
                        }
                    } else {
                        sysDictDetail = setValueToDetail(sysDictOne,list);
                    }
                }else {
                    sysDictDetail = setValueToDetail(sysDictOne,list);
                }
            }
        }

        //单表情况下
        if (DictFromType.SINGLE_TABLE.toString().equals(sysDictOne.getFromType().toString())) {

            String sql = "";
            if (!StringUtil.isEmpty(filterQueryRequest.getDictName())) {
                sql = "select " + sysDictOne.getUseForKey() + "," + sysDictOne.getUseForName() + " from " +
                        "" + sysDictOne.getFromTable() + " where data_status = '0' and " + sysDictOne.getUseForName() + "" +

                        " like '%" + filterQueryRequest.getDictName() + "%'";
            } else {
                sql = "select " + sysDictOne.getUseForKey() + "," + sysDictOne.getUseForName() + " from " +
                        "" + sysDictOne.getFromTable() + " where data_status = '0'";
            }

            List<LinkedHashMap<String, Object>> listQue = this.baseMapper.queryForSigleTable(sql);

            setObjectToItem(list, listQue, sysDictOne);

            sysDictDetail = setValueToDetail(sysDictOne,list);
        }

        //多表情况下
        if (DictFromType.TABLES_OR_VIEW.toString().equals(sysDictOne.getFromType().toString())) {

            String sql = "";

            if (!StringUtil.isEmpty(filterQueryRequest.getDictName())) {

                String FilterName = sysDictOne.getSqlFilter().substring(sysDictOne.getSqlFilter().indexOf("_NAME") - 15, sysDictOne.getSqlFilter().indexOf("_NAME") + 6);

                FilterName = FilterName.substring(FilterName.indexOf(".") - 2, FilterName.length()).trim();

                sql = sysDictOne.getSqlFilter() + "and " + FilterName + " like '%" + filterQueryRequest
                        .getDictName()
                        + "%'";
            } else {
                sql = sysDictOne.getSqlFilter();
            }
            List<LinkedHashMap<String, Object>> listQue = this.baseMapper.queryForSigleTable(sql);

            setObjectToItem(list, listQue, sysDictOne);

            sysDictDetail = setValueToDetail(sysDictOne,list);
        }

            return sysDictDetail;
        }

        private SysDictHandelDetail setValueToDetail(SysDict sysDictOne,List<Object> list){

            if (null == sysDictOne){
                return null;
            }

            SysDictHandelDetail sysDictHandelDetail = new SysDictHandelDetail();
            sysDictHandelDetail.setChildren(list);
            sysDictHandelDetail.setGuid(sysDictOne.getGuid());
            sysDictHandelDetail.setDictKey(sysDictOne.getDictKey());
            sysDictHandelDetail.setDictName(sysDictOne.getDictName());
            sysDictHandelDetail.setDictType(sysDictOne.getDictType());
            sysDictHandelDetail.setFromType(sysDictOne.getFromType());

            return sysDictHandelDetail;
        }


    @Override
    public SysDict setDefaultValue(SysDictDefaultValueRequest sysDictDefaultValueRequest) throws SysManagementException {

        SysDict sysDictQuery = selectById(sysDictDefaultValueRequest.getGuid());

        SysDictItem sysDictItem = iSysDictItemService.selectById(sysDictDefaultValueRequest.getSysDictItemGuid());

        if (null != sysDictItem){
            if (!sysDictQuery.getGuid().equals(sysDictItem.getGuidDict())){
                throw new SysManagementException(SYSExceptionCodes.SYS_DICT_ITEN_NOT_BLONG_SYS_DICT,wrap
                        ("该字典项不属于对应的业务字典"));
            }

            sysDictQuery.setDefaultValue("sys_dict_item."+sysDictItem.getItemValue());
            updateById(sysDictQuery);
        }else {
            throw new SysManagementException(SYSExceptionCodes.NOT_FOUND_SYS_DICT_ITEM,wrap("找不到字典项或已被删除"));
        }

        return sysDictQuery;
    }

    /**
     * 根据key值查询业务字典
     */
    @Override
    public SysDict queryDictByKey(String dictKey) throws SysManagementException {

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.eq(SysDict.COLUMN_DICT_KEY,dictKey);

        SysDict sysDict = selectOne(wrapper);
        return sysDict;
    }

    @Override
    public List<SysDict> queryDictExcept(String id) throws SysManagementException {

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.notIn(SysDict.COLUMN_GUID,id);

        return selectList(wrapper);
    }

    @Override
    public List<SysDictApiDetail> queryDictSon(String dictKey) throws SysManagementException {

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.eq(SysDict.COLUMN_DICT_KEY,dictKey);
        SysDict sysDict = selectOne(wrapper);
        if (null == sysDict){
            throw new SysManagementException(SYSExceptionCodes.NOT_FOUND_SYS_DICT);
        }

        List<SysDictApiDetail> apiDetailList = new ArrayList<SysDictApiDetail>();
        //查询子字典项
        Wrapper<SysDict> sonWrapper = new EntityWrapper<SysDict>();
        sonWrapper.eq(SysDict.COLUMN_GUID_PARENTS,sysDict.getGuid());

        for (SysDict sysDictSon : selectList(sonWrapper)){
            if (null != sysDictSon){
                SysDictApiDetail sysDictApiDetail = new SysDictApiDetail(sysDictSon.getDictKey(),sysDictSon
                        .getDictName(), YON.NO);
                apiDetailList.add(sysDictApiDetail);
            }
        }

        //查询字典项
        Wrapper<SysDictItem> itemWrapper = new EntityWrapper<SysDictItem>();
        itemWrapper.eq(SysDictItem.COLUMN_GUID_DICT,sysDict.getGuid());

        String dictDefaultValue = "";
        if (null != sysDict.getDefaultValue()){
            dictDefaultValue = sysDict.getDefaultValue().substring(sysDict.getDefaultValue().indexOf(".")+1,sysDict
                    .getDefaultValue().length());
        }
        for (SysDictItem sysDictItem : iSysDictItemService.selectList(itemWrapper)){
            if (null != sysDictItem){
                if (StringUtil.isEmpty(dictDefaultValue)){
                    SysDictApiDetail sysDictApiDetail = new SysDictApiDetail(sysDictItem.getItemValue(),sysDictItem
                            .getItemName(),YON.NO);
                    apiDetailList.add(sysDictApiDetail);
                }else {
                    if (dictDefaultValue.equals(sysDictItem.getItemValue())){
                        SysDictApiDetail sysDictApiDetail = new SysDictApiDetail(sysDictItem.getItemValue(),sysDictItem
                                .getItemName(),YON.YES);
                        apiDetailList.add(sysDictApiDetail);
                    }else {
                        SysDictApiDetail sysDictApiDetail = new SysDictApiDetail(sysDictItem.getItemValue(),sysDictItem
                                .getItemName(),YON.NO);
                        apiDetailList.add(sysDictApiDetail);
                    }
                }
            }
        }

        return apiDetailList;
    }

    /**
     * 判断 key值是否已经存在
     */
    @Override
    public Boolean existAddDictKey(String dictKey) throws SysManagementException {

        Boolean existKey = false;

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.eq(SysDict.COLUMN_DICT_KEY,dictKey);

        List<SysDict> sysDictList = selectList(wrapper);
        if (0 != sysDictList.size()){
            existKey = true;
        }

        return existKey;
    }

    /**
     * 判断 key值是否已经存在
     */
    @Override
    public Boolean existUpdateDictKey(String dictKey, String guid) throws SysManagementException {
        Boolean existKey = false;

        Wrapper<SysDict> wrapper = new EntityWrapper<SysDict>();
        wrapper.eq(SysDict.COLUMN_DICT_KEY,dictKey);

        List<SysDict> sysDictList = selectList(wrapper);
        if (0 != sysDictList.size()){
            SysDict sysDict = selectById(guid);
            if (null != sysDict){
                sysDictList.remove(sysDict);
                if (0 != sysDictList.size()){
                    existKey = true;
                }
            }
        }

        return existKey;
    }


    /**
     * 对外服务
     * 查询业务字典
     */
    @Override
    public Page<SysDictDetail> getSysDict(Page<SysDict> page, Wrapper<SysDict> wrapper) throws SysManagementException {

        /** 获取分页信息 */
        Page<SysDict> sysDictPage = selectPage(page,wrapper);

        /** 拼接返回结果 */
        List<SysDictDetail> sysDictDetails = getDictToDictDetail(sysDictPage.getRecords());

        Page<SysDictDetail> detailPage = new Page<>();
        detailPage.setTotal(sysDictPage.getTotal());
        detailPage.setSize(sysDictPage.getSize());
        detailPage.setCurrent(sysDictPage.getCurrent());
        detailPage.setRecords(sysDictDetails);

        return detailPage;
    }

    /**
     * 将查询回来的业务字典信息转换为返回结果
     */
    private List<SysDictDetail> getDictToDictDetail(List<SysDict> list){

        if (0 == list.size()){
            return null;
        }

        List<SysDictDetail> sysDictDetails = new LinkedList<>();
        for (SysDict sysDict : list){
            if (null != sysDict){
                SysDictDetail sysDictDetail = new SysDictDetail(sysDict);
                sysDictDetails.add(sysDictDetail);
            }
        }
        return sysDictDetails;
    }

}
